<?php
		
	/* SVN FILE: $Id: tagging.php 8 2011-03-13 08:32:38Z michele.andreoletti@gmail.com $ */
	
	/**
	* Project Name : arcadia
	*
	* @author $Author: michele.andreoletti@gmail.com $
	* @version $Revision: 8 $
	* @lastrevision $Date: 2011-03-13 08:32:38 +0000 (Sun, 13 Mar 2011) $
	* @filesource $URL: http://arcadia.googlecode.com/svn/trunk/include/tagging.php $
	*/
		
	date_default_timezone_set('Europe/Rome');

	if(isset($_GET['sAPP']) && !empty($_GET['sAPP'])) {
		$sAPP = $_GET['sAPP'];	
	} else {
		die('ERROR: App not defined');
	}

	require_once "../$sAPP/include/const.inc.php";
	require_once "../$sAPP/include/functions.php";
	require_once "auth.inc.php";
	require_once "conn.inc.php";
		
	fnGetValueFromGET(array('sAction','iTag','iView','sViewItemLink','sTable','sAPP'));
	
	if (!isset($sViewItemLink) || empty($sViewItemLink)) {
		fnEvaluateTablesAccessLvl($aTABLE_SECURITY_CHECK, false);
		if(!isset($sTable) || empty($sTable) || !isset($aTABLE[$sTable])) {
			$sTable = fnTableDefault($sTABLE_DEFAULT, $aTABLE);
		}
		fnTableConst($sTable);
	}
	
	fnViewConst($sViewItemLink);

	$sScript = fnCurrentScript();
	
	if (!empty($iView)) {
		
		$aData = fnCheckView($iView, $sViewSelectFrom, $sViewSelectGroupBy, 'aData');
	
		if (!empty($aData)) { // esiste un record associato a $iView
			$iAccessLvl = fnViewAccessLvl($iView, $aTABLE, $aTABLE_SECURITY_CHECK);
			$iFieldAccessLvl = fnFieldAccessLvl($iAccessLvl, 0, 'tags');
			if ($bTagsShow && is_allowed('edit', $iFieldAccessLvl)) {
				switch ($sAction) {
						case "tags-add-view":
						case "tags-remove-view":
						case "tags-add-table":
						case "tags-remove-table":
						
							if ($iTag != 0) { 
					
								if (!fnTagsCheck($iTag, $sViewItemLink, 1)) {
									$html = fnTagsAction($sAction, $iTag, $iView, $sViewItemLink, $sAPP);
									$sOutput = fnJsonOutput($sScript, $sAction, $html, 0);
								
								} else {
									fnUpdateLog('TAG NOT FOUND ['.abs($iTag).']', $iView, $sViewSelectFrom, 'E');
									$sOutput = fnJsonOutput($sScript, $sAction, '', 222);
								
								}
							
							} else {
								fnUpdateLog('TAG ID EMPTY', $iView, $sViewSelectFrom, 'E');
								$sOutput = fnJsonOutput($sScript, $sAction, '', 200);
					
								// SVILUPPI FUTURI...
								// Qui potrei aggiungere un'istruzione per cui, se $iTag == 0 e
								// $sAction == 'TagsAdd', rimuovo tutti i tag assegnati alla scheda
					
							}

							break;
							
						default:
							fnUpdateLog('ACTION NOT VALID', $iView, $sViewSelectFrom, 'E');
							$sOutput = fnJsonOutput($sScript, $sAction, '', 101);				
							break;				
		
				}
		
			} else {
				fnUpdateLog('LOW RIGHTS', $iView, $sViewSelectFrom, 'S');
				$sOutput = fnJsonOutput($sScript, $sAction, '', 999);

			}
		
		} else {
			fnUpdateLog('RECORD NOT FOUND', $iView, $sViewSelectFrom, 'E');
			$sOutput = fnJsonOutput($sScript, $sAction, '', 111);

		}
		
	} else {
		fnUpdateLog('RECORD ID EMPTY', $iView, $sViewSelectFrom, 'E');
		$sOutput = fnJsonOutput($sScript, $sAction, '', 100);

	}		
	
	echo $sOutput;
	die();
	
?>
